package com.chinasofti.framework.android.adapter;

import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class ListViewAdapter extends SimpleAdapter {
	
	private int[] m_to;
	private String[] m_from;
	private ViewBinder m_viewBinder;
	private List<? extends Map<String, ?>> m_data;
	private int m_resource;
	private LayoutInflater m_inflater;

	public ListViewAdapter(
			Context context,
			List<? extends Map<String, ?>> data, 
			int resource, 
			String[] from,
			int[] to) {
		super(context, data, resource, from, to);
		m_data = data;
		m_resource = resource;
		m_from = from;
		m_to = to;
		m_inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	/**
	* @see android.widget.Adapter#getView(int, View, ViewGroup)
	*/
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return createViewFromResource(position, convertView, parent, m_resource);
	}

	private View createViewFromResource(int position, View convertView,
			ViewGroup parent, int resource) {
		View view;
	
		if (convertView == null) {
			view = m_inflater.inflate(resource, parent, false);

			final int[] to = m_to;
			final int count = to.length;
			final View[] holder = new View[count];
			
			for (int i = 0; i < count; i++) {
				holder[i] = view.findViewById(to[i]);
			}
			view.setTag(holder);
		} 
		else {
			view = convertView;
		}
	
		bindView(position, view);
		return view;
	}


	private void bindView(int position, View view) {
			final Map<?, ?> dataSet = m_data.get(position);
		if (dataSet == null) {
			return;
		}
		final ViewBinder binder = m_viewBinder;
		final View[] holder = (View[]) view.getTag();
		final String[] from = m_from;
		final int[] to = m_to;
		final int count = to.length;


		for (int i = 0; i < count; i++) {
			final View v = holder[i];
			if (v != null) {
				final Object data = dataSet.get(from[i]);
				String text = data == null ? "" : data.toString();
				if (text == null) {
					text = "";
				}
				boolean bound = false;
				if (binder != null) {
					bound = binder.setViewValue(v, data, text);
				}
				if (!bound) {
					if (v instanceof Checkable) {
						if (data instanceof Boolean) {
							((Checkable) v).setChecked((Boolean) data);
						} 
						else {
							throw new IllegalStateException(v.getClass()
									.getName()
									+ " should be bound to a Boolean, not a "
									+ data.getClass());
						}
					} 
					else if (v instanceof TextView) {
						// Note: keep the instanceof TextView check at the
						// bottom of these
						// ifs since a lot of views are TextViews (e.g.
						// CheckBoxes).
						// setViewText((TextView) v, text);
						((TextView) v).setText(text);
					} 
					else if (v instanceof ImageView) {
						if (data instanceof Integer) {
							setViewImage((ImageView) v, (Integer) data);
						} 
						else if (data instanceof byte[]) { // 备注1
							Bitmap bmp;
							byte[] image = (byte[]) data;
							if (image.length != 0) {
								bmp = BitmapFactory.decodeByteArray(image, 0,
										image.length);
								((ImageView) v).setImageBitmap(bmp);
							}
						}
					} 
					else if (v instanceof RatingBar) {
						float score = Float.parseFloat(data.toString()); // 备注2
						((RatingBar) v).setRating(score);
					} 
					else {
						throw new IllegalStateException(
								v.getClass().getName()
								+ " is not a "
								+ " view that can be bounds by this SimpleAdapter");


					}
				}
			}
		}
	}


	public void setViewImage(ImageView v, int value) {
		v.setImageResource(value);
	}
}